<?php

namespace App\Http\Controllers\Backend\Permissions;

use Illuminate\Http\Request;
use App\Http\Models\Permissions\PermissionFrom;
use App\Http\Models\Permissions\PermissionsFrom;
use App\Http\Models\Permissions\PermissionsService;
use App\Http\Controllers\Backend\BackendController;

/*
 * PermissionsController  权限操作类
 *
 * index() 权限列表
 * show() 权限查看
 * create() 渲染权限新增模板
 * store() 权限新增
 * edit() 渲染权限修改模板
 * update() 权限修改
 * destroy() 删除权限
 *
 * @author Jinfeihu
 *
 * */

class PermissionsController extends BackendController
{
    private $permissionsService;
    private $request;

    public function __construct(PermissionsService $permissionsService, Request $request)
    {
        $this->permissionsService = $permissionsService;
        $this->request = $request;
    }

    /**
     * 权限列表.
     * * */
    public function index()
    {
        $permissionsDatas = $this->permissionsService->index($this->request);

        return view('backend.permissions.index', [
            'permissionsDatas' => $permissionsDatas,
            'request' => $this->request,
        ]);
    }

    /**
     * 渲染权限新增模板
     * * */
    public function create()
    {
        $menus = $this->permissionsService->getAllMenusHtml();

        return view('backend.permissions.create', [
            'menusDatas' => $menus,
        ]);
    }

    /**
     * 新增权限.
     */
    public function store(PermissionsFrom $permissionsFrom)
    {
        $this->permissionsService->store($permissionsFrom);
    }

    /**
     * 权限查看.
     *
     * */
    public function show($id)
    {
        $permissionsDatas = $this->permissionsService->show($id);

        return view('backend.permissions.show', [
            'permissionsDatas' => $permissionsDatas,
        ]);
    }

    /**
     * 菜单详情.
     */
    public function menusView($id)
    {
        $permissionsDatas = $this->permissionsService->menusView($id);

        return view('backend.permissions.show', [
            'permissionsDatas' => $permissionsDatas,
        ]);
    }

    /**
     * 渲染修改权限模板
     * * */
    public function edit($id)
    {
        $permissionsDatas = $this->permissionsService->menusView($id);

        return view('backend.permissions.update', [
            'permissionsDatas' => $permissionsDatas,
        ]);
    }

    /**
     * 修改权限.
     * * */
    public function update(PermissionFrom $permissionFrom, $id)
    {
        $this->permissionsService->updateSave($permissionFrom, $id);
    }

    /**
     * 权限删除.
     * * */
    public function destroy($id)
    {
        return $this->permissionsService->remove($id);
    }

    public function permissionsView($id)
    {
        $permissionsData = $this->permissionsService->show($id);

        return view('backend.permissions.permissionsCreate', [
            'permissionsData' => $permissionsData,
        ]);
    }

    public function permissionsCreate(PermissionFrom $permissionFrom)
    {
        $this->permissionsService->permissionsCreate($permissionFrom);
    }

    public function __destruct()
    {
        unset($this->permissionsService);
        unset($this->request);
    }
}
